home *** CD-ROM | disk | FTP | other *** search
/ InfoMagic Internet Tools 1993 July / Internet Tools.iso / RockRidge / mail / mmdf / mmdf-IIb.43 / lib / dial / d_parse.c < prev    next >
Encoding:
C/C++ Source or Header  |  1986-06-04  |  3.9 KB  |  191 lines

  1. #
  2. # include  <ctype.h>
  3. # include  <stdio.h>
  4. # include  "d_proto.h"
  5. # include  "d_returns.h"
  6.  
  7. extern int d_nfields;
  8. extern char *d_fields[];
  9. extern char *strcpy();
  10. extern char *strncpy();
  11.  
  12. /*
  13.  *     D_LINPARSE
  14.  *
  15.  *     routine which takes a line and breaks it into fields on blanks.  it
  16.  *     simply accumulates strings embedded in quotes without regard to blanks
  17.  *     until the closing quote.  the input string is altered in place by the
  18.  *     routine by the intsertion of nulls where there was blanks.
  19.  *
  20.  *     fields - pointer to array which will be loaded with pointers to the
  21.  *              fields in the input string.
  22.  *
  23.  *     linebuf - pointer to the input line buffer
  24.  *
  25.  *     maxfields - the maximum number of fields allowed on a line
  26.  *
  27.  *     the routine returns -1 if there are more than the indicated maximum
  28.  *     number of fields; -2 for unclosed quoted strings.  in the normal
  29.  *     case the number of fields isolated is returned.
  30.  */
  31.  
  32. d_linparse(fields, linebuf, maxfields)
  33.   char  *fields[], linebuf[];
  34.   int  maxfields;
  35.     {
  36.     char *cp, *ncp;
  37.     register int nfields, result;
  38.  
  39.     nfields = 0;
  40.  
  41.     if ( (result = d_dollar(linebuf)) < 0)
  42.     return (result);
  43.  
  44.     cp = linebuf;
  45.  
  46.     for (;;)
  47.     {
  48.     result = d_getword(cp, &fields[nfields], &ncp);
  49.     switch (result)
  50.     {
  51.         case -1:
  52.         /*  end of line  */
  53.         return (nfields);
  54.  
  55.         case -2:
  56.         /*  unterminated quotes  */
  57.         return (-2);
  58.  
  59.         default:
  60.         /*  another call, another word  */
  61.         break;
  62.     }
  63.     if (++nfields > maxfields)
  64.         return (-1);
  65.     cp = ncp;
  66.     }
  67. }
  68.  
  69.  
  70.  
  71. d_getword (linebuf, start, next)
  72.   char *linebuf;
  73.   char **start, **next;
  74.     {
  75.     register char *cp;
  76.  
  77.     cp = linebuf;
  78.  
  79.     /*  skip over any leading white space  */
  80.     while ((*cp == ' ') || (*cp == '\t'))
  81.         *cp++ = '\0';
  82.  
  83.     /*  return -1 to indicate that there was no word  */
  84.     if ((*cp == '\n') || (*cp == '\0'))
  85.     {
  86.         *cp = '\0';
  87.         return (-1);
  88.     }
  89.  
  90.     /*  save the pointer to the start of the field  */
  91.     *start = cp;
  92.  
  93.     /*  special case for quoted strings.  embedded blanks are
  94.      *  ignored.  must find another '"' before the newline or null.
  95.      */
  96.     if (*cp == '"')
  97.     {
  98.         cp++;
  99.  
  100.         while ((*cp != '"') && (*cp != '\n') && (*cp != '\0'))
  101.             cp++;
  102.  
  103.         if ((*cp == '\n') || (*cp == '\0'))
  104.             return(-2);
  105.  
  106.         cp++;
  107.     }
  108.     else
  109.         while ((*cp != ' ') && (*cp != '\t') &&
  110.                (*cp != '\n') && (*cp != '\0'))
  111.             cp++;
  112.     *cp++ = '\0';
  113.     *next = cp;
  114.     return (0);
  115. }
  116.  
  117.  
  118.  
  119. d_dollar(line)
  120.   char *line;
  121.     {
  122.     char templine[MAXSCRLINE], number[10];
  123.     register char *cp;
  124.     register int index;
  125.     int changes = 0;
  126.     int len = 0;
  127.  
  128.     cp = line;
  129.  
  130.     for ( ; ;)
  131.     {
  132.       switch (*cp)
  133.       {
  134.     case '\0':
  135.     case '\n':
  136.          if (changes)
  137.          {
  138.         templine[len++] = '\0';  
  139.             (void) strcpy(line, templine);
  140.             line[len] = '\0'; /* parser requires 2 nulls */
  141.          }
  142.  
  143.          return(0);
  144.  
  145.     case '$': 
  146.          changes++;
  147.          cp++;
  148.  
  149.          if (*cp == '$')  /* double $$ expands to $ */
  150.          {
  151.             if (addout(templine, &len,  cp++, 1))
  152.             return(-5);
  153.             break;
  154.          }
  155.  
  156.               /* gobble up number and expand */
  157.          for (index = 0; 
  158.           isdigit(*cp) && (*cp!='\0') && (*cp!='\n') && (index<10);
  159.           index++)
  160.                  number[index] = *cp++;
  161.          number[++index] = '\0';
  162.               index = atoi(number);
  163. #ifdef D_DBGLOG
  164.          d_dbglog("d_dollar", "variable number: %d, max: %d", 
  165.               index, d_nfields);
  166. #endif
  167.               if ((index <= 0) || (index > d_nfields))
  168.                  return(-4);
  169.          if (addout(templine,&len, d_fields[index],strlen(d_fields[index])))
  170.         return(-5);
  171.          break;
  172.  
  173.     default:
  174.          if (addout(templine, &len, cp++, 1))
  175.             return(-5);
  176.              break;
  177.        }
  178.    }        
  179. }        
  180.  
  181. addout(buf, len, str, cnt)
  182. char *buf, *str;
  183. int *len, cnt;
  184. {
  185.     if (*len + cnt >= MAXSCRLINE - 2)
  186.     return(1);
  187.     (void) strncpy(&buf[*len],str,cnt);
  188.     *len += cnt;    
  189.     return(0);
  190. }
  191.